import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {DynamicComponent, DynamicComponentEvent} from '../../../../../ng-tools';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {TicketService} from '../../../../services/api/ticket.service';
import {HttpParams} from '@angular/common/http';

@Component({
  selector: 'edit-robot',
  templateUrl: './robot-edit.component.html',
  styleUrls: ['./robot-edit.component.scss']
})
export class RobotEditComponent implements OnInit, DynamicComponent {
  @ViewChild('fileInput') fileInput;
  @ViewChild('imageElement') imageElement;
  context: any;
  dynamic: true;
  events: DynamicComponentEvent;
  currentRobot;
  @Output() success = new EventEmitter();
  set data(value: any) {
    this.currentRobot = value;
  }
  robotForm: FormGroup;
  imageUrl;
  constructor(private formBuilder: FormBuilder,
              private ticketService: TicketService) {
    this.robotForm = this.formBuilder.group({
      userId: ['', Validators.required],
      nickName: ['', Validators.required],
      whatsUp: ['', Validators.required],
    });
  }

  ngOnInit() {
    const defaultValue = {
      userId: this.currentRobot.userUid,
      nickName: this.currentRobot.nickName,
      whatsUp: this.currentRobot.whatSUp,
    };
    this.robotForm.patchValue(defaultValue);
    this.imageUrl = this.ticketService.serverConfig.httpServer + this.ticketService.baseUrl + this.ticketService.urls.userAvatarDownloader + '?action=ad&user_uid=' + this.currentRobot.userUid + '&enforceDawnload=1';
    this.downLoadAvatar();
  }
  downLoadAvatar() {
    this.ticketService.get(this.ticketService.urls.userAvatarDownloader + '?action=ad&user_uid=51942797&enforceDawnload=1').subscribe((resp) => {
    }, (error) => {
    });
  }
  changeAvatar() {
    this.fileInput.nativeElement.click();
  }
  selectFile(e) {
    const formData = new FormData();
    formData.append('user_uid', this.currentRobot.userUid);
    formData.append('file_name', e.target.files[0].name);
    formData.append('file',  e.target.files[0]);
    this.ticketService.post(this.ticketService.urls.UserAvatarUploader, formData, {
      observe: 'response',
      params: new HttpParams().append('web_token', localStorage.getItem('token'))}).subscribe((resp) => {
      if (resp.body.success) {
        this.imageUrl = this.ticketService.serverConfig.httpServer + this.ticketService.baseUrl + this.ticketService.urls.userAvatarDownloader + '?action=ad&user_uid=' + this.currentRobot.userUid + '&enforceDawnload=1';
        this.imageElement.nativeElement.src = this.imageUrl;
      }
    });
  }
  trigger(eventName: string, ...args) {
    this[eventName].emit(...args);
    if (eventName in this.events) {
      this.events[eventName](...args);
    }
  }
  update() {
    if (this.robotForm.valid) {
      this.ticketService.post(this.ticketService.urls.updateRobotInfo, this.robotForm.value, {
        observe: 'response',
        params: new HttpParams().append('web_token', localStorage.getItem('token'))}).subscribe((resp) => {
        if (resp.body.success) {
          this.trigger('success');
          }
      });
    }
  }
}
